package com.alt.www.generic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 27340 on 2017/7/30.
 */
public class Favorites {
    private Map<Class<?>, Object> favorites = new HashMap<Class<?>,Object>();
    public <T> void putFavorite(Class<T> type, T instance){
        if(type == null)
            throw new NullPointerException();
        favorites.put(type, type.cast(instance));
    }
    public <T> T getFacorite(Class<T> type){
        return type.cast(favorites.get(type));
    }

    public static void main(String[] args){
        Favorites f = new Favorites();
        Object s1 = 1;
        String s2 = "sss";
        List<Integer> list = new ArrayList<Integer>();
        unsafeAdd(list,s2);
        f.putFavorite(Integer.class,list.get(0));
    }

    private static void unsafeAdd(List list, Object o){
        list.add(o);
    }

}
